//
//  ActivitiesListViewController.m
//  UI_豆瓣
//
//  Created by dllo on 15/10/26.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "ActivitiesListViewController.h"
#import "ActivitiesTableViewCell.h"
#import "ActivitiesDetailViewController.h"
#import "ActivitesList.h"
#import "AppTools.h"
#import "MBProgressHUD.h"
#import "DataBaseSingleng.h"
@interface ActivitiesListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, retain)UITableView *actTV;
@property (nonatomic, retain)NSMutableArray *actListArray;
@property (nonatomic, retain) MBProgressHUD *hub;
@end

@implementation ActivitiesListViewController

- (void)dealloc
{
    [_hub release];
    [_actListArray release];
    [_actTV release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self.actTV reloadData];
    
    //注册cell
    [self.actTV registerClass:[ActivitiesTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    //设置标题
    self.navigationItem.title = @"活动";

    
    //初始化数组
    self.actListArray = [NSMutableArray array];
    
    //让菊花旋转起来
    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hub show:YES];
    self.hub.labelText = @"玩命加载ing......";
    [self.view addSubview:self.hub];

    
//    //搜索沙盒
//    NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
//    
//    NSString *actArray = [document stringByAppendingPathComponent:@"actListArray.plist"];
//    //反归档取本地
//    NSMutableDictionary *actDic = [NSKeyedUnarchiver unarchiveObjectWithFile:actArray];
//    //扒开文件
//    NSMutableArray *array = [actDic objectForKey:@"events"];
//    for (NSMutableDictionary *dic in array) {
//        
//        ActivitesList *act = [[ActivitesList alloc]init];
//        [act setValuesForKeysWithDictionary:dic];
//        [self.actListArray addObject:act];
////        NSLog(@"1111111%@",self.actListArray);
//    }
    
    
    //请求数据
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/activitylist.php";
    
   
    
    [AppTools getDataWithUrlStr:urlStr andBlock:^(id result) {
        
        NSMutableDictionary *dic = result;
        NSString *event = [dic objectForKey:@"events"];
        NSMutableArray *array = [dic objectForKey:@"events"];
        
            //删除表(删除上次运行留下的表)
            [[DataBaseSingleng shareDataBase]dropTable];
            
            //创建表
            [[DataBaseSingleng shareDataBase]createActivityListTable];

        
        for (NSMutableDictionary *dic in array) {
            ActivitesList *activityList = [[ActivitesList alloc]init];
            [activityList setValuesForKeysWithDictionary:dic];
            [self.actListArray addObject:activityList];
        
            [[DataBaseSingleng shareDataBase]insertActivityListSQL:activityList];
        }
        
        //查询
    self.actListArray = [[DataBaseSingleng shareDataBase]selectActivity];
        
        //让菊花停止
        [self.hub hide:YES];
        
        //刷新
        [self.actTV reloadData];
  
    } error:^(NSError *error) {
        
        //让菊花停止
        [self.hub hide:YES];

    }];
        
    
}
    

//懒加载 创建tableView

-(UITableView *)actTV
{
    if (_actTV == nil) {
        self.actTV = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
        self.actTV.delegate = self;
        self.actTV.dataSource = self;
        [self.view addSubview:self.actTV];
        [_actTV release];
    }
    return _actTV;
}

#pragma mark -- 设置区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return  1;
}

#pragma mark -- 设置行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.actListArray.count;
}

#pragma mark -- 设置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    ActivitiesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    ActivitesList *act = [self.actListArray objectAtIndex:indexPath.row];
    
    cell.activities = act;
    
    NSLog(@"12121%@",cell.activities.image_hlarge);
    return cell;
}

#pragma mark -- 设置cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 183;
}

#pragma mark -- 设置触摸是触发方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //点中置灰  放开取消置灰
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    ActivitesList *act = [self.actListArray objectAtIndex:indexPath.row];
    
    ActivitiesDetailViewController *actDetaVC = [[ActivitiesDetailViewController alloc]init];
    //传值
    actDetaVC.actList = act;
    
    //隐藏详情界面的TabBar
    [actDetaVC setHidesBottomBarWhenPushed:YES];

    [self.navigationController pushViewController:actDetaVC animated:YES];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
